package org.tbjd.client.guis.taskList;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.customWidgets.taskList.TreeGrid;
import org.tbjd.client.customWidgets.taskList.TreeGridItem;
import org.tbjd.client.exceptions.NoUpperDecisionmakerFoundException;
import org.tbjd.client.exceptions.NoUpperMediatorFoundException;
import org.tbjd.client.exceptions.TBJDException;
import org.tbjd.client.interfaces.Decisionmaker;
import org.tbjd.client.interfaces.Displayer;
import org.tbjd.client.interfaces.HasDragController;
import org.tbjd.client.interfaces.HasMediator;
import org.tbjd.client.interfaces.Observer;
import org.tbjd.client.mediators.AbstractMediator;

import com.allen_sauer.gwt.dnd.client.DragController;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class TaskListGui extends Composite implements Observer, Displayer, HasMediator, Decisionmaker, HasDragController {

	static private TaskListGui instance = null;
	private VerticalPanel container = null;

	private TreeGrid treeGrid = null;

	private AbstractMediator mediator = null;

	private TaskListGui() {
		build();
		initWidget(this.container);
	}

	private void build() {
		this.container = new VerticalPanel();
		this.container.setStyleName("tbjd-taskList");

		treeGrid = new TreeGrid(getColumnHeader());
		String[] columnsWidth = { "200px", "50px", "50px" };

		try {
			treeGrid.setColumnsWidth(columnsWidth);
		} catch (Exception e) {
			e.printStackTrace();
		}

		this.container.add(treeGrid);

	}

	static public TaskListGui getInstance() {
		if (instance == null) {
			instance = new TaskListGui();
		}
		return instance;
	}

	public void display() {
	}

	public void display(List<?> items) {
		System.out.println("TaskListGui.display() ");
	}

	public void setMediator(AbstractMediator mediator) {
		this.mediator = mediator;
		this.mediator.addObserver(this);
	}

	public void setDragController(DragController dragController) {
	}

	public void decide(Object action) {
		System.out.println("TaskListGui.decide()");
		try {
			delegate(action);
		} catch (NoUpperDecisionmakerFoundException e) {
			e.printStackTrace();
		} catch (NoUpperMediatorFoundException e) {
			e.printStackTrace();
		}
	}

	public void delegate(Object action) throws NoUpperDecisionmakerFoundException, NoUpperMediatorFoundException {
		System.out.println("TaskListGui.delegate()");
		try {
			this.mediator.decide(action);
		} catch (TBJDException e) {
			e.printStackTrace();
		}
	}

	public void handle(Object action) {
	}

	public void update(Object sender) {
		System.out.println("TaskListGui.update()");
		if (((Map<?, ?>) sender).containsKey(NotificationsConstants.LIST_TASKS)) {
			this.treeGrid.setTreeGridItems((List<TreeGridItem>) ((Map<?, ?>) sender).get(NotificationsConstants.LIST_TASKS));
		}
	}

	private ArrayList<Widget> getColumnHeader() {
		ArrayList<Widget> columnHeader = new ArrayList<Widget>();

		columnHeader.add(new Label("Task"));
		columnHeader.add(new Label("Left"));
		columnHeader.add(new Label("%"));
		return columnHeader;
	}

}
